package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.mybeans.form.FormBeanFactory;

import model.Model;
import model.TokboxDAO;

import databean.Tokbox;
import databean.User;
import formbean.VideoSaveConfNotesForm;


public class VideoSaveConfNotesAction extends Action {
	
	private FormBeanFactory<VideoSaveConfNotesForm> formBeanFactory = FormBeanFactory.getInstance(VideoSaveConfNotesForm.class);
	
	private TokboxDAO tokboxDAO;


	public VideoSaveConfNotesAction(Model model) {
		tokboxDAO = model.getTokboxDAO();
	}

	public String getName() { return "video-save-conf-notes.do"; }

	public String perform(HttpServletRequest request,HttpServletResponse response) {
        
		// Set up the errors list
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        Tokbox tokbox;
        
        HttpSession session = request.getSession();
        
		try {
	        
			if (session.getAttribute("user") == null) {
				return "user-login.do";
			}
	        
			request.setAttribute("user",(User) request.getSession(false).getAttribute("user"));
			
			
			////////////////////////
			//saving notes section//
			////////////////////////
	
			VideoSaveConfNotesForm form = formBeanFactory.create(request);
			request.setAttribute("form",form);
	
			// Any validation errors?
			errors.addAll(form.getValidationErrors());
			if (errors.size() != 0) {
				tokbox = tokboxDAO.getTokboxByConfName(form.getConfName());
				request.setAttribute("tokbox", tokbox);

				return "video-existing-confs.do";
			}

			tokbox = tokboxDAO.getTokboxByConfName(form.getConfName());
			tokbox.setNotes(form.getNotes());
			tokboxDAO.update(tokbox);
			
			
			return "video-existing-confs.do";
	    	
	        
	        
        } catch (Exception e) {
        	errors.add(e.getMessage());
        	return "video-existing-confs.do";
        }
        
    }
	
}



